#!/usr/bin/perl
use strict;
use warnings;
############################################################

$ARGV[0] || die usage();

if ($ARGV[0] eq "-m") {
    my @val = parse_moss();
    pp_res (@val);
}
elsif ($ARGV[0] eq "-mf") {
    my @val = parse_moss();
    my @perc = freq(@val);
    pp_percentage (@perc);
} 
elsif ($ARGV[0] eq "-j"){
    pp_res (parse_jplag());
}
elsif ($ARGV[0] eq "-s"){
    pp_res (parse_sherlock());
}
elsif ($ARGV[0] eq "-jf"){
    my @perc = freq (parse_jplag());
    pp_percentage (@perc);
}
elsif ($ARGV[0] eq "-f"){
    my @perc = freq (parse_res());
    pp_percentage (@perc);
}
elsif ($ARGV[0] eq "-min"){
    shift @ARGV;
    my @names;
    while(@ARGV){
	push (@names, $ARGV[0]);
	shift @ARGV;
    }
    find_res_min (@names);
}
elsif ($ARGV[0] eq "-filter"){
    shift @ARGV;
    my @names;
    while(@ARGV){
	push (@names, $ARGV[0]);
	shift @ARGV;
    }
    filter (@names);
}
elsif ($ARGV[0] eq "-diff"){
    shift @ARGV;
    my @names;
    while(@ARGV){
	push (@names, $ARGV[0]);
	shift @ARGV;
    }
    diff (@names);
}
elsif ($ARGV[0] eq "-pd"){
    parse_diff();
}
elsif ($ARGV[0] eq "-cm"){
    shift @ARGV;
    my @res_files;
    my @names;
    while(@ARGV){
	if ($ARGV[0] =~ /(..*\.res)/){
	    my @res = read_res ("$1");
	    push (@res_files, \@res);
	}
	elsif ($ARGV[0] =~ /(\d\d*)/) {
	    push (@names, $1);
	}
	else {
	    print "### Invalid Argument to Cross min\n";
	}
	shift @ARGV;
    }
    cross_min (\@res_files, \@names);
}
else {
    usage ();
}


sub usage {
    print "Usage: ./extractor <param>\n";
    print "<param>\n";
    print "-m    Parse moss file (Pasted from browser) and return .res style \n";
    print "-mf   Parse moss file (Pasted from browser) and return frequency vals\n";
    print "-j    Parse a jplag file and return .res style \n";
    print "-jf   Parse a jplag file and return frequency vals \n";
    print "-f    Parse res file and return frequency vals\n";
    print "-min  Return min vals foreach project from .res file \n";
    print "-cm   Cross min \n";
    print "-s    Parse Sherloc file and return .res style\n ";
}

## Print result 
sub pp_res {
    for (my $i = 0; $i < scalar(@_); $i += 3){
	print "(", $_[$i], ",", $_[$i+1], ") ", sprintf("%.3f", $_[$i+2]) ,"\n";
    }
}

## Read from stdin the jplag out 
sub parse_jplag {
    my @iface;
    while (<STDIN>) {
	if (/(\d{1,2}).*<TH>(\d{1,2})/){
	    #print "(", $1, ",", $2, ") ";
	    my $first = $1;
	    my $second = $2;
	    if ($_ =~ /\((\w{1,2}\.\d)%\).*\((\w{1,2}\.\d)%\)/){
		my $norm_val;
		if ($1 > $2){
		    $norm_val = 1 - $1 / 100;
		}
		else {
		    $norm_val = 1 - $2 / 100;
		}
		push (@iface, $first);
		push (@iface, $second);
		push (@iface, $norm_val);
	    }
	}    
    }
    return @iface;
}

sub parse_sherlock {
    my @iface;
    while (<STDIN>) {
	if (/(\d{1,2})\..* (\d{1,2})..*: (..*)/){
	    print "(", $1, ",", $2, ")  $3\n";
	    push (@iface, $1);
	    push (@iface, $2);
	    push (@iface, $3);
	}    
    }
    return @iface;
}

## Parse MOSS result (Pasted from browser)
sub parse_moss {
    my @iface;
    while (<STDIN>) {
	if (/\/(\d{1,2})\/.*\/(\d{1,2})\//){
	    my $first = $1;
	    my $second = $2;
	    if ($_ =~ /\((\w{1,2})%\).*\((\w{1,2})%\)/){
		my $norm_val;
		if ($1 > $2){
		   $norm_val = 1 - ($1 / 100);
		}else {
		   $norm_val = 1 - ($2 / 100);
		}
		push (@iface, $first);
		push (@iface, $second);
		push (@iface, $norm_val);
	    }
	}
    }
    return @iface;
}

## Print percentage
sub pp_percentage {
    for (my $i = 0; $i < 10; $i++){
	print $i*10, "%-", ($i+1)*10, "% ", $_[$i], "\n";
    }
}

sub freq {
    my @iface = @_;
    my $norm_val;
    my @percentage = (0,0,0,0,0,0,0,0,0,0);

    for (my $i = 2; $i < @iface; $i+=3){
	$norm_val = $iface [$i];
	if ($norm_val <= 0.1){
	    $percentage[0] += 1;
	} elsif ($norm_val <= 0.2){
	    $percentage[1] += 1;
	} elsif ($norm_val <= 0.3){
	    $percentage[2] += 1;
	} elsif ($norm_val <= 0.4){
	    $percentage[3] += 1;
	} elsif ($norm_val <= 0.5){
	    $percentage[4] += 1;
	} elsif ($norm_val <= 0.6){
	    $percentage[5] += 1;
	} elsif ($norm_val <= 0.7){
	    $percentage[6] += 1;
	} elsif ($norm_val <= 0.8){
	    $percentage[7] += 1;
	} elsif ($norm_val <= 0.9){
	    $percentage[8] += 1;
	} elsif ($norm_val <= 1){
	    $percentage[9] += 1;
	}
    }
    return @percentage;
}

## Read the res out from stdin
sub parse_res {
    my @iface;

    while (<STDIN>){
	if (/\((.*),(.*)\) (.*)/){
	    push (@iface, $1);
	    push (@iface, $2);
	    push (@iface, $3);
	}
    }
    return @iface;
}

sub read_res {
    my @iface;    
    my $data_file = "@_";
    open FILE, "<@_" or die $!;
    
    while (<FILE>){
	if (/\((.*),(.*)\) (.*)/){
	    push (@iface, $1);
	    push (@iface, $2);
	    push (@iface, $3);
	}
    }
    return @iface;
    close(FILE);
    
}

## 

sub find_res_min {
    my @project = @_;
    my @iface = parse_res();
    foreach (@project){
	my $proj = $_;
	#print "For project  $_ \n";
	my @min = (-1,-1,3);	
	for (my $i = 0; $i < @iface; $i+= 3) {
	    if ($proj == $iface[$i] || $proj == $iface[$i + 1]){
		if ($iface [$i + 2] < $min [2]){
		    @min = ($iface [$i], $iface [$i + 1], $iface [$i + 2]);   
		}
	    }
	}
	if ($min[1] != -1){
	    print "(", $min[0], ",", $min[1], ") ", sprintf("%.3f", $min[2]);
	    print " $_", "\n";
        }
        else {
	    print "(", $_, ",", "-1", ") ", sprintf("%.3f", "1.0");
	    print " $_", "\n";
        }	
    }
}

sub cross_min {

    my ($res_files, $names) = @_;
    my @names = @$names;
    my @res_files = @$res_files;
    #my @res;

#     foreach (@res_files) {
# 	@res = @$_;
# 	pp_res (@res);
#    }

    my @project = @names;

    my $ifacer = @$res_files[0];
    my @iface = @$ifacer;
    shift @res_files;
    # Per la media
    my $num_val = 1;
    my $mean = 0;
    # ########## #

    foreach (@project){
	my $proj = $_;
	#print "For project  $_ \n";
	my @min = (-1,-1,3);
	$mean = 0;
	$num_val = 1;
	for (my $i = 0; $i < @iface; $i+= 3) {
	    if ($proj == $iface[$i] || $proj == $iface[$i + 1]){
		if ($iface [$i + 2] < $min [2]){
		    @min = ($iface [$i], $iface [$i + 1], $iface [$i + 2]);   
		}
	    }
	}
	if ($min[1] != -1){
	    print "(", $min[0], ",", $min[1], ") ", sprintf("%.3f", $min[2]);
	    $mean += $min[2];
	    ## cerca il min negli atri file 
	    foreach (@res_files) {
		my @res = @$_;
		my $val = 1;
		for (my $i = 0; $i < scalar(@res); $i += 3){
		    if (($min[0] == $res[$i] && $min[1] == $res[$i+1]) || ($min[0] == $res[$i+1] && $min[1] == $res[$i])){
			$val = $res[$i+2];
		    }
		}
		print " (", $min[0], ",", $min[1], ") ", sprintf("%.3f", $val);
		$mean += $val;
		$num_val += 1;
	    }
	    #print " $_", "\n";
	    print " ", $mean / $num_val;
	    print "\n";
        }
        else {
	    print "(", $_, ",", "-1NOT_FOUND", ") ", sprintf("%.3f", "1");
	    print " $_", "\n";
	    ## scrivi 
        }	
    }
}

sub filter {
    my @names = @_;
    while (<STDIN>) {
	if (/\((.*),(.*)\) *(.*)/){
	    foreach (@names) {
		if ($_ == $1 || $_ == $2){
		    print "(", $1, ",", $2, ") ",$3, "\n"; 
		}   
	    }
	} 
    }
}

sub diff {
    my @names = @_;
    foreach (@names){
	my $name = $_;
	foreach (@names){
	    if ($name < $_){
		my $diff = "sdiff ../Tests/$name.if ../Tests/$_.if -I -b -W -B -s | wc -l ";
		my $total = "sdiff ../Tests/$name.if ../Tests/$_.if -I -b -W -B | wc -l ";
		print "($name,", $_,")";
		system ("$diff && $total");
	    }
	}
    }
}

sub parse_diff {
    my @iface;
    my $diff = 0;
    my $tot = 0;
    my $val = 0;
    while (<STDIN>) {
	if (/\((.*),(.*)\) *(.*)/){
	    $diff = $3;
	    print "(", $1, ",", $2, ") ";
	} elsif (/ *(..*)/){
	    $tot = $1;
	    $val = $diff / $tot;
	    print sprintf("%.3f", $val)," \n";
	}
    }
    return @iface;
}
